package com.ssyc.gsk_parents.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.bean.ResultInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 填写回执
 * Created by Administrator on 2018/6/20 0020.
 */

public class ParentsFillReceiptActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mIvBack;
    private EditText mEtInput;
    /**
     * 0/500
     */
    private TextView mTvNum;
    private Button mBtSave;


    //上传回执需要的rowId
    private String rowId;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_fill_receipt;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //获取传递的rowId
        initIntent();
    }


    /**
     * 初始化视图
     */
    public void initView() {
        mIvBack = findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(this);
        mEtInput = findViewById(R.id.et_input);
        mTvNum = findViewById(R.id.tv_num);
        mBtSave = findViewById(R.id.bt_save);
        mBtSave.setOnClickListener(this);
        mEtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = mEtInput.getText().toString().trim();
                if (content.length() > 500) {
                    mTvNum.setText("500/500");
                    UiUtils.Toast("最多只能输入500字", false);
                    return;
                } else {
                    mTvNum.setText(content.length() + "/500");
                }
            }
        });
    }

    /**
     * 获取传递的rowId
     */
    private void initIntent() {
        rowId = getIntent().getStringExtra("rowid");
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.bt_save) {
            // 保存
            doCommitAction();
        }
    }


    /**
     * 上传回执的操作
     */
    private void doCommitAction() {
        String content = mEtInput.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UiUtils.Toast("请先填写内容", false);
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("type", "42");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
//        map.put("acc", "13035149941");
//        map.put("role", "2");
//        map.put("apptoken", "WHBHWJDSAKDAKSDNSAJDAK");
        map.put("platform", "8");
        map.put("rowId", rowId);
        map.put("content", content);

        CustomDialogManager.show(this, "提交中...");

        HttpUtils.post(HttpAdress.FILLRECEIPT, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }


                    if (null != info) {
                        if ("200".equals(info.state)) {
                            //返回成功
                            finish();
                            UiUtils.Toast("提交成功", false);

                            //提交成功刷新数据
                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(ParentsScoreInquiryActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);


                        } else {
                            UiUtils.Toast("提交失败,返回错误码:" + info.state, false);
                            Log.i("test", "返回器返回异常,错误码是:" + info.state);
                        }
                    }
                } else {
                    Log.i("test", "服务返回数据异常");
                }
            }
        });
    }
}
